package com.mp3player.util;

import android.content.Context;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.mp3player.model.Mp3Info;
import com.mp3player.mp3player.R;
import com.mp3player.xml.Mp3ListContentHandler;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.StringReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by Cuckoo322 on 3/20/2017.
 */

public class ToolUtils {
    
    /**
     * 利用mp3列表信息展示在ListView中
     * @param mp3InfoList mp3列表信息
     */
    public static void renderMp3ListView(List<Mp3Info> mp3InfoList, Context context, ListView mp3ListView) {
        List<HashMap<String, String>> rows = new ArrayList<>();
        for (Iterator it = mp3InfoList.iterator(); it.hasNext();) {
            Mp3Info mp3Info = (Mp3Info) it.next();
            HashMap<String, String> row = new HashMap<>();
            row.put(MetaData.MP3_NAME_COLUMN, mp3Info.getMp3Name());
            row.put(MetaData.MP3_SIZE_COLUMN, ToolUtils.byte2Mb(mp3Info.getMp3Size()));
            rows.add(row);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(context
                , rows
                , R.layout.mp3info_item
                , new String[]{MetaData.MP3_NAME_COLUMN, MetaData.MP3_SIZE_COLUMN}
                , new int[]{R.id.mp3_name, R.id.mp3_size});
        mp3ListView.setAdapter(simpleAdapter);
    }

    /**
     * 将byte字节大小的字符串转化成以Mb为单位并精确到小数点后2位的字符串
     * @param byteStr 字节大小的数字字符串
     * @return 以Mb为单位并精确到小数点后2位的字符串
     */
    public static String byte2Mb(String byteStr) {
        DecimalFormat df = new DecimalFormat("###.##");
        Integer byteNum = 0;
        try {
            byteNum = Integer.valueOf(byteStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String mb = String.valueOf(df.format((double) byteNum / (1024.0 * 1024.0))) + "Mb";
        return mb;
    }

    /**
     * 解析字符串中的XML文本
     * @param xmlStr 字符串形式的XML文本
     * @return mp3信息列表
     */
    public static List<Mp3Info> parse(String xmlStr) {
//        System.out.println("start parsing...");
        //获得SAX解析器工厂实例
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        List<Mp3Info> mp3InfoList = new ArrayList<>();
        try {
            //从SAX解析器工厂获得xml扫描器
            XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
            //新建xml内容解析器
            Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(mp3InfoList);
            //设置xml扫描器的内容解析器
            xmlReader.setContentHandler(mp3ListContentHandler);
            //开始解析xml
            xmlReader.parse(new InputSource(new StringReader(xmlStr)));
            //解析完成，获得结果
            for (Iterator it = mp3InfoList.iterator(); it.hasNext();) {
                Mp3Info mp3Info = (Mp3Info) it.next();
//                System.out.println(mp3Info);
            }
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mp3InfoList;
    }

    /**
     * 将毫秒数转成##:##的字符串格式，由于精确到秒，剩余的毫秒数将被忽略
     * @param milliseconds 毫秒数
     * @return ##:##的字符串格式
     */
    public static String milliseconds2TimeString (int milliseconds) {
        DecimalFormat df = new DecimalFormat("##");
        int minutes = 0;
        int seconds = 0;
        minutes = milliseconds / (60 * 1000);
        milliseconds = milliseconds % (60 * 1000);
        seconds = milliseconds / (1000);
        return df.format(minutes)+":"+df.format(seconds);
    }

}
